#论坛系统注册页

from common.com import ForumDriver
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException


class ForumRegister:
    driver = ""
    url = ""
    def __init__(self):
        self.url = "http://127.0.0.1:58080/sign-up.html"
        self.driver = ForumDriver.driver
        self.driver.get( self.url )

    def RegisterFailTest(self):
            # 1. 用户名已存在
            self.driver.find_element( By.CSS_SELECTOR, "#username" ).send_keys( "xiaohe1" )
            self.driver.find_element( By.CSS_SELECTOR, "#nickname" ).send_keys( "xiaohe1" )
            self.driver.find_element( By.CSS_SELECTOR, "#password" ).send_keys( "123456" )
            self.driver.find_element( By.CSS_SELECTOR, "#passwordRepeat" ).send_keys( "123456" )
            #点击同意使用条款按钮
            self.driver.find_element( By.CSS_SELECTOR, "#policy" ).click()
            #点击注册按钮
            self.driver.find_element( By.CSS_SELECTOR, "#submit" ).click()
            ForumDriver.getScreenShot()  # 截图
            # 验证登陆失败（出现弹窗)
            try:
                # 等待弹窗出现（设置超时时间，如3秒，与弹窗自动消失时间匹配或稍短）
                alert = WebDriverWait(self.driver,3 ).until(EC.alert_is_present())
                # 获取弹窗文本并打印
                alert_text = alert.text
                print( f"弹窗内容: {alert_text}" )
            except TimeoutException:
                print( "等待弹窗超时，未检测到弹窗" )


            #2.输入用户名、昵称、密码、确认密码，不勾选协议，点击注册
            self.driver.find_element( By.CSS_SELECTOR, "#username" ).clear()
            self.driver.find_element( By.CSS_SELECTOR, "#nickname" ).clear()
            self.driver.find_element( By.CSS_SELECTOR, "#password" ).clear()
            self.driver.find_element( By.CSS_SELECTOR, "#passwordRepeat" ).clear()
            checkbox = self.driver.find_element( By.CSS_SELECTOR, "#policy" )
            if checkbox.is_selected():  # 检查协议框是否被选中
                checkbox.click()  # 取消勾选
            self.driver.find_element( By.CSS_SELECTOR, "#username" ).send_keys( "xiaohe1" )
            self.driver.find_element( By.CSS_SELECTOR, "#nickname" ).send_keys( "xiaohe1" )
            self.driver.find_element( By.CSS_SELECTOR, "#password" ).send_keys( "123456" )
            self.driver.find_element( By.CSS_SELECTOR, "#passwordRepeat" ).send_keys( "123456" )
            self.driver.find_element( By.CSS_SELECTOR, "#submit" ).click()
            ForumDriver.getScreenShot()  # 截图
            # 检查协议是否勾选
            checkbox = self.driver.find_element( By.CSS_SELECTOR, "#policy" )
            assert checkbox.is_selected() == False


            # 3.输入用户名、昵称、密码、确认密码，确认密码与密码不一致
            self.driver.find_element( By.CSS_SELECTOR, "#username" ).clear()
            self.driver.find_element( By.CSS_SELECTOR, "#nickname" ).clear()
            self.driver.find_element( By.CSS_SELECTOR, "#password" ).clear()
            self.driver.find_element( By.CSS_SELECTOR, "#passwordRepeat" ).clear()
            checkbox = self.driver.find_element( By.CSS_SELECTOR, "#policy" )
            if checkbox.is_selected():  # 检查协议复选框是否被选中
                checkbox.click()  # 取消勾选
            self.driver.find_element( By.CSS_SELECTOR, "#username" ).send_keys( "xiaohe1" )
            self.driver.find_element( By.CSS_SELECTOR, "#nickname" ).send_keys( "xiaohe1" )
            self.driver.find_element( By.CSS_SELECTOR, "#password" ).send_keys( "123456" )
            self.driver.find_element( By.CSS_SELECTOR, "#passwordRepeat" ).send_keys( "123" )
            self.driver.find_element( By.CSS_SELECTOR, "#policy" ).click()
            self.driver.find_element( By.CSS_SELECTOR, "#submit" ).click()
            ForumDriver.getScreenShot()  # 截图
            text = self.driver.find_element( By.CSS_SELECTOR, "#signUpForm > div > div:nth-child(5) > div > div" ).text
            assert text == "请检查确认密码"


            # 4.不输入确认密码, 输入用户名、昵称、密码，勾选协议，点击注册
            self.driver.find_element( By.CSS_SELECTOR, "#username" ).clear()
            self.driver.find_element( By.CSS_SELECTOR, "#nickname" ).clear()
            self.driver.find_element( By.CSS_SELECTOR, "#password" ).clear()
            self.driver.find_element( By.CSS_SELECTOR, "#passwordRepeat" ).clear()
            checkbox = self.driver.find_element( By.CSS_SELECTOR, "#policy" )
            if checkbox.is_selected():  # 检查协议复选框是否被选中
                checkbox.click()  # 取消勾选
            self.driver.find_element( By.CSS_SELECTOR, "#username" ).send_keys( "xiaohe1" )
            self.driver.find_element( By.CSS_SELECTOR, "#nickname" ).send_keys( "xiaohe1" )
            self.driver.find_element( By.CSS_SELECTOR, "#password" ).send_keys( "123456" )
            self.driver.find_element( By.CSS_SELECTOR, "#policy" ).click()
            self.driver.find_element( By.CSS_SELECTOR, "#submit" ).click()
            ForumDriver.getScreenShot()  # 截图
            text = self.driver.find_element( By.CSS_SELECTOR, "#signUpForm > div > div:nth-child(5) > div > div" ).text
            #assert text == "请检查确认密码"



    def RegisterSuTest(self):
        # 清理输入框和 取消勾选协议
        self.driver.find_element( By.CSS_SELECTOR, "#username" ).clear()
        self.driver.find_element( By.CSS_SELECTOR, "#nickname" ).clear()
        self.driver.find_element( By.CSS_SELECTOR, "#password" ).clear()
        self.driver.find_element( By.CSS_SELECTOR, "#passwordRepeat" ).clear()
        checkbox = self.driver.find_element( By.CSS_SELECTOR, "#policy" )
        if checkbox.is_selected():  # 检查复选框是否被选中
            checkbox.click()  # 取消勾选
        # 输入用户名、昵称、密码、确认密码，且密码与确认密码一致，勾选协议，点击注册
        self.driver.find_element( By.CSS_SELECTOR, "#username" ).send_keys( "xiaohe1" )
        self.driver.find_element( By.CSS_SELECTOR, "#nickname" ).send_keys( "xiaohe1" )
        self.driver.find_element( By.CSS_SELECTOR, "#password" ).send_keys( "123456" )
        self.driver.find_element( By.CSS_SELECTOR, "#passwordRepeat" ).send_keys( "123456" )
        # 勾选同意协议
        self.driver.find_element( By.CSS_SELECTOR, "#policy" ).click()
        # 点击注册
        self.driver.find_element( By.CSS_SELECTOR, "#submit" ).click()
        # 跳转到登录页面
        text = self.driver.find_element( By.CSS_SELECTOR,"body > div > div > div > div:nth-child(1) > div > div.card.card-md > div > h2" ).text
        assert text == "用户登录"


